<template>
  <div class="flexContent">
    <div class="searchBox">
      <el-form ref="formRef" class="demo-form-inline" @submit.native.prevent label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="24">
              <el-col :span="8">
                <el-form-item label="字典类型名称">
                  <el-input v-model.trim="name" style="width: 70%" placeholder="请输入..." clearable
                    @keyup.enter.native="selByTypeFn" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="更新时间">
                  <el-date-picker style="width: 70%" clearable v-model="date" type="daterange" unlink-panels
                    range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" />
                </el-form-item>
              </el-col>

            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="selByTypeFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetFn" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="add">
      <el-button class="addBtn" @click="addFn">新增</el-button>
      <el-button class="addBtn" @click="getDictValue">加载字典</el-button>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <el-table class="tablebox" ref="isExpand" row-key="id" stripe :data="dicList" style="width: 100%" height="100%"
          size="medium">
          <el-table-column type="expand" class="childtable" label="-">
            <template slot-scope="props">
              <el-table ref="multipleTable" :data="props.row.sysDictList" style="width: 100%; margin-left: 0"
                class="tablebox">
                <el-table-column prop="name" label="字典名称" align="center" />
                <el-table-column prop="code" label="字典编码(Key)" align="center" />
                <el-table-column prop="value" label="字典值(Value)" align="center" />
                <el-table-column prop="info" label="最近修改" align="center">
                  <template v-slot="{ row }">
                    {{ row.updateName }}/{{ row.updateTime | parseTime }}
                  </template>
                </el-table-column>
                <el-table-column prop="info" label="创建" align="center">
                  <template v-slot="{ row }">
                    {{ row.createName }}/{{ row.createTime | parseTime }}
                  </template>
                </el-table-column>
                <!-- <el-table-column prop="info" label="操作" align="center">
                <template v-slot="{ row }">
                  <div>
                    <el-button
                    type="text"
                    size="big"
                    style="margin-right: 10px; color: red"
                    @click.stop="delChildDic(row)"
                    >删除字典</el-button>
                  </div>
                </template>
              </el-table-column> -->
              </el-table>
            </template>
          </el-table-column>
          <el-table-column prop="code" label="字典类型编号" align="center" />
          <el-table-column prop="name" label="字典类型名称" align="center" />
          <el-table-column prop="updateTime" label="更新时间" align="center">
            <template v-slot="{ row }">
              {{ row.updateTime | parseTime }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" align="center">
            <template v-slot="{ row }">
              {{ row.createTime | parseTime }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template v-slot="{ row }">
              <el-button type="text" size="big" style="margin-right: 10px; color: rgb(0, 132, 255)"
                @click.stop="addChildDic(row)">新增枚举</el-button>
              <!-- <el-button
              type="text"
              size="big"
              style="margin-right: 10px; color: red"
              @click.stop="delParentDic(row)"
              >删除字典</el-button
            > -->
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,->, sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
    <AddDialog :close-on-click-modal="false" :show-dialog.sync="showDialog" :width="width" :title="title" @btnOk="btnOk"
      @close="close">
      <el-form ref="addFormRef" :model="addForm" :rules="addRules" label-width="120px">
        <el-form-item label="字典类型编号" style="width: 100%" prop="code">
          <el-input v-model="addForm.code" />
        </el-form-item>
        <el-form-item label="字典类型名称" style="width: 100%" prop="name">
          <el-input v-model="addForm.name" />
        </el-form-item>
      </el-form>
    </AddDialog>
    <el-dialog :title="title2" :visible.sync="showDialog2" width="40%" :before-close="closeChild"
      :close-on-click-modal="false">
      <el-form ref="addFormRef2" :model="addForm2" :rules="addForm2Rules" label-width="120px">
        <el-form-item style="width: 100%" label="字典类型编号" prop="typeCode">
          <el-input v-model="addForm2.typeCode" disabled />
        </el-form-item>
        <el-form-item label="字典类型名称" style="width: 100%" prop="typeName">
          <el-input v-model="addForm2.typeName" disabled />
        </el-form-item>
        <el-form-item label="字典名称" style="width: 100%" prop="name">
          <el-input v-model="addForm2.name" />
        </el-form-item>
        <el-form-item label="字典编码(Key)" style="width: 100%" prop="code">
          <el-input v-model="addForm2.code" />
        </el-form-item>
        <el-form-item label="字典值(Value)" style="width: 100%" prop="value">
          <el-input v-model="addForm2.value" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeChild" style="width: 88px;">返 回</el-button>
        <el-button class="submitBtn" style="width: 88px;" @click="addChildDicFn">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getDictValue } from "@/api/dictionary";
import AddDialog from "@/components/Goods/AddDialog";
import {
  getDicList,
  addDicInfoParent,
  updateMenuDetail,
  deleteDicInfoParent,
  addDicInfoChild,
  deleteDicInfoChild,
  refreshDict,
} from "@/api/system";
export default {
  name: "DictionaryInfo",
  components: {
    AddDialog,
  },
  data() {
    return {
      loading: false,
      width: "40%",
      title: "新增",
      title2: "新增枚举",
      showDialog: false,
      showDialog2: false,
      isSearch: false,
      addForm: {
        code: "",
        name: "",
      },
      addRules: {
        code: [
          {
            required: true,
            message: "请输入字典类型编号",
            trigger: "change",
          },
          {
            min: 1,
            max: 50,
            message: "长度在1到50个字符",
            trigger: "change",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入字典类型名称",
            trigger: "change",
          },
          {
            min: 1,
            max: 50,
            message: "长度在1到50个字符",
            trigger: "change",
          },
        ],
      },
      addForm2: {
        typeCode: "",
        typeName: "",
        code: "",
        name: "",
        value: "",
      },
      addForm2Rules: {
        typeCode: [
          {
            required: true,
            message: "请输入字典类型编号",
            trigger: "change",
          },
        ],
        typeName: [
          {
            required: true,
            message: "请输入字典类型名称",
            trigger: "change",
          },
        ],
        code: [
          {
            required: true,
            message: "请输入字典编码",
            trigger: "change",
          },
          {
            min: 1,
            max: 50,
            message: "长度在1到50个字符",
            trigger: "change",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入字典名称",
            trigger: "change",
          },
          {
            min: 1,
            max: 50,
            message: "长度在1到50个字符",
            trigger: "change",
          },
        ],
        value: [
          {
            required: true,
            message: "请输入字典值",
            trigger: "change",
          },
          {
            pattern: /^([0-9])+$/,
            message: "只能输入数字",
            trigger: "change",
          },
          {
            min: 1,
            max: 20,
            message: "长度在1到20个字符",
            trigger: "change",
          },
        ],
      },
      dicList: [],
      name: "",
      date: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      pId: "",
      childId: "",
    };
  },
  created() {
    this.getDicList();
  },
  methods: {
    async getDictValue() {
      const res = await getDictValue();
      if (res.code === 200) {
        this.$message.success("刷新成功");
      } else {
        // this.$message.error(res.messgae);
      }
    },
    // 获取字典列表
    async getDicList() {
      if (!this.isSearch) {
        this.name = "";
        this.openTime = "";
        this.endTime = "";
      }
      const res = await getDicList({
        ...this.page,
        name: this.name,
        openTime: this.openTime,
        endTime: this.endTime,
      });
      if (res.code === 200) {
        const result = res.data;
        this.dicList = result.list;
        this.page.pageSize = result.pageSize;
        this.page.pageNum = result.pageNum;
        this.page.total = result.total;
        console.log("dicList", this.dicList);
      } else {
        // this.$message.error(res.message);
      }
    },
    // 更新字典
    async refreshDict() {
      const res = await refreshDict();
      console.log(res);
    },
    addFn(row) {
      this.title = "新增";
      this.showDialog = true;
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      this.getDicList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pageNum = val;
      this.getDicList();
    },
    close() {
      this.showDialog = false;
      this.addForm = {
        code: "",
        name: "",
      };
      this.$refs.addFormRef.resetFields();
    },
    btnOk() {
      // 判断是新增还是修改
      if (this.title === "新增") {
        // 新增
        this.$refs.addFormRef.validate(async (isOk) => {
          if (isOk) {
            //
            const res = await addDicInfoParent({ ...this.addForm });
            if (res.code === 200) {
              this.$message.success("新增成功");
              this.resetPage(this.page);
              this.getDicList();
              this.close();
              this.refreshDict();
            } else {
              // this.$message.error(res.message);
            }
          } else {
            this.$message.warning("请检查输入信息");
          }
        });
      } else if (this.title === "详情") {
        // 关闭
        this.close();
      } else if (this.title === "修改") {
        // 修改
        this.$refs.addFormRef.validate(async (isOk) => {
          if (isOk) {
            const res = await updateMenuDetail({
              ...this.addForm,
              id: this.pId,
            });
            if (res.code === 200) {
              this.$message.success("修改成功");
              this.getDicList();
              this.close();
            } else {
              // this.$message.error(res.message);
            }
          } else {
            this.$message.warning("请检查输入信息");
          }
        });
      }
    },
    async edit(row) {
      this.showDialog = true;
      this.title = "修改";
      console.log(row);
      this.addForm.code = row.code;
      this.addForm.name = row.name;
      this.pId = row.id;
    },
    selByTypeFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.endTime = this.date?.length ? this.date[1] : "";
      this.openTime = this.date?.length ? this.date[0] : "";
      this.isSearch = true;
      this.getDicList();
    },
    addChildDic(row) {
      this.showDialog2 = true;
      this.title2 = "新增枚举";
      console.log(row, "数据");
      this.addForm2.typeCode = row.code;
      this.addForm2.typeName = row.name;
      // this.addForm2.childDicList = []
      // row.sysDictList.forEach(item => {
      //   this.addForm2.childDicList.push(item)
      // })
      this.pId = row.id;
    },
    delParentDic(row) {
      this.$confirm(`此操作将永久删除该字典, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await deleteDicInfoParent({ id: row.id, typeCode: row.typeCode });
        if (res.code === 200) {
          if (this.dicList.length === 1 && this.page.pageNum > 1)
            this.page.pageNum--;
          this.getDicList();
        } else {
          // this.$message.error(res.message);
        }
      }).catch(() => {
      })
    },
    delChildDic(row) {
      this.$confirm(`此操作将永久删除该字典, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await deleteDicInfoChild({ id: row.id, typeCode: row.typeCode });
        if (res.code === 200) {
          this.$message.success("删除成功");
          this.getDicList();
        } else {
          // this.$message.error(res.message);
        }
      }).catch(() => {
      })
    },
    closeChild() {
      this.showDialog2 = false;
      this.addForm2 = {
        typeCode: "",
        typeName: "",
        code: "",
        name: "",
        value: "",
      };
      this.$refs.addFormRef2.resetFields();
      // this.getDicList()
    },
    addChildDicFn() {
      this.$refs.addFormRef2.validate(async (isOk) => {
        if (isOk) {
          //
          const res = await addDicInfoChild({ ...this.addForm2 });
          if (res.code === 200) {
            this.$message.success("新增成功");
            this.getDicList();
            this.closeChild();
            this.refreshDict();
          } else {
            // this.$message.error(res.message);
          }
        } else {
          this.$message.warning("请检查输入信息");
        }
      });
    },
    resetFn() {
      this.name = "";
      this.date = [];
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = false;
      this.getDicList();
    },
  },
};
</script>

<style lang="scss" scoped></style>
